package com.yitao.cms.service.user.appUser;

import com.google.common.collect.Lists;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.user.UserAddressManagementDto;
import com.yitao.client.model.user.QUserAddressManagementModel;
import com.yitao.client.model.user.UserAddressManagementModel;
import com.yitao.cms.constant.BaseEnum;
import com.yitao.cms.vo.user.appUser.AppUserAddressVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.List;

@Service
public class AppUserAddressService extends GenericEntityService<UserAddressManagementModel, UserAddressManagementDto, QUserAddressManagementModel> {
    public AppUserAddressService(EntityManager entityManager) {
        super(entityManager);
    }

    /**
     * 根据userId查询收货地址
     *
     * @param userId 地址id
     * @return
     */
    public List<AppUserAddressVo> findAddressByUserId(Long userId) {
        List<UserAddressManagementModel> userAddressManagementModels = repository.findAll(Q.userId.eq(userId).and(Q.isDelete.eq(false)));
        List<AppUserAddressVo> appUserAddressManagementVoList = Lists.newArrayList();
        if (userAddressManagementModels != null && userAddressManagementModels.size() != 0) {
            AppUserAddressVo appUserAddressVo = null;
            for (UserAddressManagementModel userAddressManagementModel : userAddressManagementModels) {
                appUserAddressVo = new AppUserAddressVo();
                BeanUtils.copyProperties(userAddressManagementModel, appUserAddressVo);
                StringBuffer stringBuffer = new StringBuffer();
                if (userAddressManagementModel.getProvince() != null) {
                    stringBuffer.append(userAddressManagementModel.getProvince());
                }
                if (userAddressManagementModel.getCity() != null) {
                    stringBuffer.append(userAddressManagementModel.getCity());
                }
                if (userAddressManagementModel.getArea() != null) {
                    stringBuffer.append(userAddressManagementModel.getArea());
                }
                if (userAddressManagementModel.getAddress() != null) {
                    stringBuffer.append(userAddressManagementModel.getAddress());
                }
                appUserAddressVo.setAddress(stringBuffer.toString());

                /**
                 * 是否默认
                 */
                if (userAddressManagementModel.getDefault() != null) {
                    if (userAddressManagementModel.getDefault()) {
                        appUserAddressVo.setIsDefault(BaseEnum.YesOrNoEnum.YES.getName());
                    } else {
                        appUserAddressVo.setIsDefault(BaseEnum.YesOrNoEnum.NO.getName());
                    }

                    appUserAddressManagementVoList.add(appUserAddressVo);
                }
            }
        }
        return appUserAddressManagementVoList;
    }

    /**
     * 根据id查询收获地址
     */
    public UserAddressManagementModel getUserAddressManagementModelById(Long id) {
        return queryFactory.select(Q).from(Q).where(Q.id.eq(id)).fetchOne();
    }


}
